<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h2>设置分数</h2>
        <form id="setGrade" enctype="multipart/form-data">
            <div class="form-group">
                <label for="grade">分数</label>
                <input type="text" class="form-control" id="grade" name="grade" required>
            </div>
            <button type="submit" class="btn btn-primary">提交</button>
        </form>
    </div>

    <!-- 使用 Fetch API 提交表单 -->
    <script>
        document.getElementById('setGrade').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            const formData = new FormData();
            formData.append('grade', document.getElementById('grade').value);
            formData.append('student_id', {{ student_id }});
            formData.append('assignment_id', {{ assignmentId }});

            // 使用 Fetch API 发送数据到后端
            fetch('/assignments/assignment/set_grade', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(data.error);
                }
                if (data.message){
                    alert('成绩设置成功！');
                    // 跳转到班级列表页面
                    window.location.href = '/assignments/assignment/assignment_status/{{ assignmentId }}' + '?class_id=' + {{ classId }};
                }
            })
            .catch(error => {
                // 捕获并显示错误信息
                alert(error.message);
            });
        });
    </script>
</body>
</html>